package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @Description: // 类说明，在创建类时要填写
 * @ClassName: OrderController    // 类名，会自动填充
 * @Author: MYH          // 创建者
 * @Date: 2022/12/25 21:49   // 时间
 * @Version: 1.0     // 版本
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;


    /**
     * 完成下单操作
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){

        //输出日志
        log.info("订单数据:{}",orders);

        orderService.submit(orders);
        return R.success("下单成功!!");
    }


    /**
     * 移动端前台订单的分页查询功能
     * @param page
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(Integer page,Integer pageSize){

        //分页构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, BaseContext.getCurrent());
        queryWrapper.orderByDesc(Orders::getCheckoutTime);

        //查询数据库
        Page<Orders> page1 = orderService.page(ordersPage,queryWrapper);

        return R.success(page1);
    }


    /**
     * 系统管理后台订单的分页查询功能
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> backPage(Integer page, Integer pageSize, Long number,String beginTime,String endTime){

        //分页构造器
        Page<Orders> page1 = new Page<>(page,pageSize);

        //条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(number != null,Orders::getId,number);
        queryWrapper.ge(beginTime != null,Orders::getOrderTime,beginTime);
        queryWrapper.le(endTime != null,Orders::getOrderTime,endTime);
        queryWrapper.orderByDesc(Orders::getOrderTime);

        //查询数据库
        Page<Orders> page2 = orderService.page(page1, queryWrapper);
        return R.success(page2);
    }


    /**
     * 完成系统管理后台的派送！！
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> finalProgress(@RequestBody Orders orders){

        orderService.updateById(orders);
        return R.success("已完成派送！!");
    }

}
